//检测小程序是否是最新版本的
const newapp = () => {
	const updateManager = uni.getUpdateManager();
	updateManager.onCheckForUpdate((res) => {});
	updateManager.onUpdateReady((res) => {
		uni.showModal({
			title: '更新提示',
			content: '新版本已经准备好，是否重启应用？',
			success: (res) => {
				if (res.confirm) {
					updateManager.applyUpdate();
				}
			}
		});
	});
	updateManager.onUpdateFailed((res) => {});
}

const handleMember = (data) => {
	const titleOrder = {
			"一天卡": 1,
			"三天卡": 3,
			"五天卡": 5,
			"周卡": 7,
			"十天卡": 10,
			"半月卡": 15,
			"月卡": 30,
			"六十天卡": 60,
			"季卡": 90,
			"半年卡": 180,
			"年卡": 360
		};
	data.forEach(item => {
		if (item.type !== 8 && item.type !== 13 && titleOrder.hasOwnProperty(item.title)) {
			item.day = titleOrder[item.title];
		}
	});

	return data;
}

// 检查是否过期
const checkDataExpiration = () => {
	const now = new Date().getTime()
	const storedTime = uni.getStorageSync('timestamp')
	if (storedTime && now - storedTime > 24 * 60 * 60 * 1000) {
		// 清除过期的数据
		uni.removeStorageSync('timestamp')
		uni.removeStorageSync('keywords')
		uni.removeStorageSync('bookId')
	}
}

/**
 * 公共跳转方法
 * @param {Object} options - 跳转所需参数对象
 * @param {string} options.id - 书籍ID
 * @param {string} options.chapter_id - 章节ID
 */
const navigateToPage = (options) => {
	// 检查是否可以使用阅读器 ，不支持则跳转到兜底页面
	if (!tt.canIUse('novel-plugin')) {
		tt.redirectTo({
			url: `/pages/Detail/Detail?id=${options.id}&chapter_id=${options.chapter_id}`,
		});
	} else {
		tt.navigateTo({
			url: `/pages/novel_plugin/index?book_id=${options.id}&chapter_id=${options.chapter_id}`,
		});
	}
}

const displayTips = (content, showCancel = false, confirmText = "去首页") => {
	uni.showModal({
		title: '温馨提示',
		content: content,
		showCancel: showCancel,
		confirmText: '去首页',
		success: (res) => {
			if (res.confirm) {
				uni.switchTab({
					url: "/pages/index/index"
				})
			}
		}
	})
}


export {
	newapp,
	handleMember,
	checkDataExpiration,
	navigateToPage,
	displayTips

}